package com.example.environment.controller;

import com.alibaba.druid.util.StringUtils;
import com.example.environment.entity.User;
import com.example.environment.entity.base.BaseRequestBean;
import com.example.environment.entity.base.BaseResponseBean;
import com.example.environment.entity.base.BaseRspPageBean;
import com.example.environment.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;



/**
 *  @author: wangbo(1141294273@qq.com)
 *  @Date: 2021/2/1 22:02
 *  @Description: 用户信息
 */
@RestController
@RequestMapping("/api")
public class UserController {
    private static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;


    /**
    * @Description 查询用户对象
    * @Author  wangbo
    * @Date   2021/2/1 22:12
    * @Param
    * @Return
    * @Exception
    *
    */
    @RequestMapping("/getUserInfo")
    public BaseResponseBean getUserInfo(@RequestBody  BaseRequestBean<User> baseRequestBean){
        User user;
        try {
            if(StringUtils.isEmpty(baseRequestBean.getData().getUid())){
                return new BaseResponseBean().isError("uid不能为空!");
            }
            user =  userService.getByExample(baseRequestBean.getData().getUid());
        } catch (Exception e) {
            logger.error("查询用户对象信息异常",e);
            return new BaseResponseBean().isError("服务器异常请联系管理员!");
        }
        return new BaseResponseBean().isSuccess(user);
    }

    @RequestMapping("/queryPageUserInfo")
    public BaseRspPageBean<User> queryPageUserInfo(@RequestBody  BaseRequestBean<User> baseRequestBean){
        BaseRspPageBean<User> baseRspPageBean = new BaseRspPageBean<>();
        try {
            baseRspPageBean =  userService.pageQueryUser(baseRequestBean.getData(),baseRequestBean.getPageNum(),baseRequestBean.getSize());

        } catch (Exception e) {
            logger.error("分页查询用户对象信息异常",e);
            baseRspPageBean.setSuccess(false);
        }
        baseRspPageBean.setSuccess(true);
        return baseRspPageBean;

    }

    @RequestMapping("/updateUserInfo")
    public BaseResponseBean updateUserInfo(@RequestBody  BaseRequestBean<User> baseRequestBean){
        boolean isUpdate;
        try {
            isUpdate =  userService.updateByUser(baseRequestBean.getData());
            if(!isUpdate){
                return new BaseResponseBean().isError("修改失败!",false);
            }
        } catch (Exception e) {
            logger.error("修改用户对象信息异常",e);
            return new BaseResponseBean().isError("服务器异常请联系管理员!");
        }
        return new BaseResponseBean().isSuccess(isUpdate);

    }

    @RequestMapping("/insertUserInfo")
    public BaseResponseBean insertUserInfo(@RequestBody  BaseRequestBean<User>  baseRequestBean){
        boolean isInsert;
        try {
            isInsert =  userService.insertSelective(baseRequestBean.getData());
            if(!isInsert){
                return new BaseResponseBean().isError("添加失败!",false);
            }
        } catch (Exception e) {
            logger.error("添加用户对象信息异常",e);
            return new BaseResponseBean().isError("服务器异常请联系管理员!");
        }
        return new BaseResponseBean().isSuccess(isInsert);

    }
}
